﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Collections.ObjectModel;
using HellCook.Model;

namespace HellCook.DataAccess
{
    public static class Herramientas
    {
        public static string ObjectToXml<T>(T objectToSerialize)
        {
            try
            {
                XmlSerializer xmlSer = new XmlSerializer(objectToSerialize.GetType());
                StringWriter sWriter = new StringWriter();                
                xmlSer.Serialize(sWriter, objectToSerialize);                
                return sWriter.ToString();
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }


        public static object XmlToObject(string xmlToDeserialize, Type type)
        {
            try
            {
                XmlSerializer xmlSer = new XmlSerializer(type);                
                StringReader sReader = new StringReader(xmlToDeserialize);                
                object retDTO = (object)xmlSer.Deserialize(sReader);                
                return retDTO;
            }
            catch (Exception ex)
            {                
                throw ex;
            }            
        }

        private static Byte[] StringToUTF8ByteArray(String pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        } 
    }
}
